var rssGrid = null;

function onsubmitFun() {
	var rssurl = frm.rssurl.value;
	if (rssurl == null || rssurl == "") {
		alert("请输入RSS的地址!")
		return;
	}
	frm.submit();
}
function initRSSGrid(ctl) {
	var columns = new Array();
	if(ctl==0){
		columns = [ {
			name : "rssName",
			caption : "RSS名称",
			width : 140,
			mode : "string",
			// mode : "link",
			// href:"javascript:alert(1);",
			align : "left"
		}];
	}else{
		columns = [ {
			name : "rssName",
			caption : "RSS名称",
			width : 140,
			mode : "string",
			// mode : "link",
			// href:"javascript:alert(1);",
			align : "left"
		}, {
			name : "opt",
			caption : "",
			width : 35,
			mode : "link",
			href : "javascript:void(1);",
			align : "left"
		} ];
	}
	
	
	var context = {
		sequence : false,
		radioBox : false,
		readonly : true,
		locked : 0,
		sortable : false,
		paintMode : "all"
	};
	
	$("rsslistdiv").innerHTML="";
	rssGrid = null;
	rssGrid = new SigmaGrid("rsslistdiv", context, columns);
	rssGrid.bindData([]);
	rssGrid.onCellClicked = rowSelect
}

function rowSelect(evt) {
	var gridCell = evt.cell;
	var gridRow = evt.cell.row;
	var url = gridRow.rowData["url"];
	var chnlid = gridRow.rowData["id"];
	unSelected();
	gridRow.setSelected(true);
	if (gridCell.getValue() == "退订"&&window.confirm("确定是否退订?") ) {
		UserManager.delChannel(uid, chnlid, function(data){
			if (data == 0) {
				// showpage();
				alert("退订成功!");
				rssGrid.deleteSelectedRows();//移出此频道
			} else {
				alert("退订失败!");
			}
		});
	}else{
	
	if (url != "") {
		$("rssurl").value = url.value;
		onsubmitFun();

		
	}
	}
}

// 取消所有已选中的行。
function unSelected() {
	var selrows = rssGrid.getSelectedRows();
	for ( var i = 0; i < selrows.length; i++) {
		selrows[i].setSelected(false);
	}

}
var rds = 0;
var uid = 0;
window.onload = function initRsslist() {
	DWRUtil.useLoadingMessage("请稍等……");
	$("ifr").style.pixelHeight =document.body.scrollHeight-100;
	$("rsslistdiv").style.height = document.body.scrollHeight-100;
	showpage();
	rdWelcome();
	//alert(document.body.scrollHeight);
	
	
	
	// dwr.engine._execute("/dwr", 'ReaderRSS', 'getRssinfoByName', "", bks);
}
//加装页面信息
function showpage(){
	var us = checkCookie();
	if(us!=0){//lgi
		initRSSGrid(1);
		var usl = us.split(":");
		uid = usl[0];
		//$("logininfo").style.display="";
		$("logonbt").style.display="";
		DWRUtil.setValue("logininfo","欢迎您的到来，"+usl[1]);
		$("loginbt").style.display="none";
		UserManager.getUserRss(uid,bks);
	}else{
		initRSSGrid(0);
		$("logonbt").style.display="none";
		//$("logininfo").style.display="";
		DWRUtil.setValue("logininfo","欢迎您的到来");
		$("loginbt").style.display="";
		ReaderRSS.getRssinfoByDef("true", bks);//读取默认的频道
	}
}
function bks(lis) {
	// alert(lis[0].url.value);
	var datas = new Array();
	for ( var i = 0; i < lis.length; i++) {
		datas[i] = {
			rssName : lis[i].rssName,
			url : lis[i].url,
			id : lis[i].id,
			opt : '退订'
		}
	}
	rssGrid.bindData(datas);
}
//订阅rss
function addRss() {
	if(uid==0){
		if(window.confirm("您还没登陆，确定是否登陆")){
			showLogin();
		}else{
			return;
		}
	}
	var rss = new Object();
	rss.url = $("rssurl").value;
	if(rss.url==null||rss.url==""){
		alert("请先填写RSS地址");
		return;
	}
	var frame = document.frames["ifr"];
	var rssName = frame.window.rssTitle.innerText;
	rss.rssName = rssName;// window.prompt("请输入RSS的名称","");

	
	if (rss.rssName != null && rss.url != null) {
		ReaderRSS.insertRssinfo(rss, function(int) {
			if (int != -1) {
//				ReaderRSS.getRssinfoByName("", bks);
				UserManager.addChannel(uid,int,addbks);
				
			} else {
				alert("订阅失败!");
			}
		});
	}
}
function addbks(data){
	if(data==0){
		showpage();
		alert("订阅成功!");
	}else{
		alert("订阅失败!");
	}
	
}
function delById() {
//	//rssGrid.onCellClicked();
//	var selRows = rssGrid.getSelectedRowsValue();
//	if(selRows==null||selRows.length==0){
//		alert("请先选择要删除的RSS!");
//		return;
//	}
//	if (window.confirm("确定是否删除?")) {
//		for ( var i = 0; i < selRows.length; i++) {
//			ReaderRSS.deleteById(selRows[i].id, function(a) {
//				if (a == 0) {
//					rssGrid.deleteSelectedRows();
//					alert("成功!");
//				} else {
//					alert("失败!");
//				}
//			});
//		}
//	}
}
//弹出注册页面。
function showSignup(){
	var targetWindowStyle = "dialogHeight: 600px; dialogWidth: 800px; center: Yes; help: No; resizable: no; status: yes; scroll:yes";
	var returnData = showModalDialog("./user/signup.html", window, targetWindowStyle);
	alreadlylogin(returnData);
//	if(typeof returnData !='undefined'){
//		uid = returnData.id;
//		showpage();
//	}
	
}
//弹出登录页面。
function showLogin(){
	var targetWindowStyle = "dialogHeight: 600px; dialogWidth: 800px; center: Yes; help: No; resizable: no; status: yes; scroll:yes";
	var returnData = showModalDialog("./user/login.html", window, targetWindowStyle);
	alreadlylogin(returnData);
}
function alreadlylogin(data){
	if(typeof data !='undefined'){
	//if(data!=null&&data!=""){
		setCookie("theuser",data.id+":"+data.nickName,365);
	}
	showpage();
}
function userlogon(){
	clearcookie();
	uid=0;
	showpage();
}
function rdWelcome(){
	var ifrw=document.frames["ifr"];
	var sHTML="<img src=\"./images/fly/DSCF{N}.JPG\">"//插入图片的HTML代码模板
	ifrw.document.write(sHTML.replace("{N}",Math.random()*3|0)) 
	/*Math.random()取到一个大于0小于1的浮点数，然后乘以7得到大于0小于7浮点数，再与0作位或运算，返回大于0小于7的整数*/
	


}